package com.oneinlet.server

import com.oneinlet.YCMessageDecoder
import com.oneinlet.YCMessageEncoder
import com.oneinlet.server.handler.AppServerHandler
import com.oneinlet.server.handler.YCServerHandler
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelFuture
import io.netty.channel.ChannelInitializer
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.timeout.IdleStateHandler
import io.netty.util.HashedWheelTimer
import io.netty.util.Timeout
import io.netty.util.Timer
import io.netty.util.TimerTask
import org.slf4j.LoggerFactory
import java.util.concurrent.TimeUnit


/**
 * Created by WangZiHe on 2019/1/7/007
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */

class YCServer(private val serverPort: Int, private val appPort: Int) {
    private val logger = LoggerFactory.getLogger("YCServer")

    // 接收用户客户端的请求访问
    fun startService() {
        val bossGroup = NioEventLoopGroup()
        val workerGroup = NioEventLoopGroup()
        val serverBootstrap = ServerBootstrap()
        try {
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel::class.java)
                    .childHandler(
                            object : ChannelInitializer<SocketChannel>() {
                                override fun initChannel(ch: SocketChannel?) {
                                    ch!!.pipeline()
                                            .addLast(AppServerHandler())
                                }
                            })
            val f: ChannelFuture = serverBootstrap.bind(appPort).sync()
            val address = f.channel().localAddress()
            logger.info("App Server has been started，address:$address ,  please enjoy it！")
            startYCService()
        } finally {
            bossGroup.shutdownGracefully()
            workerGroup.shutdownGracefully()
            logger.warn("App server has been shutdown， EventLoopGroup  has been released all resources！")
        }
    }


    private fun startYCService() {
        val bossGroup = NioEventLoopGroup()
        val workerGroup = NioEventLoopGroup()
        val serverBootstrap = ServerBootstrap()
        try {
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel::class.java)
                    .childHandler(
                            object : ChannelInitializer<SocketChannel>() {
                                override fun initChannel(ch: SocketChannel?) {
                                    ch!!.pipeline()
                                            .addLast(IdleStateHandler(25, 20, 0))
                                            .addLast(YCMessageDecoder())
                                            .addLast(YCMessageEncoder())
                                            .addLast(YCServerHandler())
                                }
                            })
            val f: ChannelFuture = serverBootstrap.bind(serverPort).sync()
            val address = f.channel().localAddress()
            logger.info("YC Server has been started，address:$address ,  please enjoy it！")
            f.channel().closeFuture().sync()
        } finally {
            bossGroup.shutdownGracefully()
            workerGroup.shutdownGracefully()
            logger.warn("YC server has been shutdown， EventLoopGroup  has been released all resources！")
        }

    }

}